package com.xiaoxu.intranetweb.mybatisplusSzappdb.controller.auth;

import com.alibaba.fastjson.JSONObject;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.auth.LoginBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.auth.UserInfoBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.auth.WebMenuBu;
import com.xiaoxu.intranetweb.util.SzResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

/**
 * 登录控制器
 * @author xiaoxu
 * @date 2022/12/25 15:00
 * @version 1.0
 * @since 1.0
 */
@CrossOrigin()
@RestController
@RequestMapping(value="/auth")
public class LoginController {
    @Autowired
    private LoginBu loginBu;
    @Autowired
    private UserInfoBu userInfoBu;
    @Autowired
    private WebMenuBu webMenuBu;
//    登录
    /**
     * 登录
     * @author xiaoxu
     * @date 2022/12/25 15:00
     * @version 1.0
     * @since 1.0
     * @param jsonParam：用户名和密码{username:xxx,password:xxx}
     * @param request：请求
     */
    @PostMapping("/login")
    public SzResult login(@RequestBody JSONObject jsonParam, HttpServletRequest request) throws Exception {
        String username = jsonParam.getString("username");
        String password = jsonParam.getString("password");
        String ip = request.getHeader("X-Real-IP");
        String plantId = request.getHeader("Plant-Id");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return loginBu.login(username, password,ip,plantId);
    }

//    退出登录
    /**
     * 退出登录
     * @param request：请求
     */
    @GetMapping("/logout")
    public SzResult logout(HttpServletRequest request) {
//        拿到token
        String token = request.getHeader("Authorization");
        return loginBu.logout(token);
    }
    /**
     * 获取用户信息
     * @param request：请求
     */
    @PostMapping("/getUserInfo")
    public SzResult getInfo(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        return userInfoBu.getUserInfo(token);
    }
    @GetMapping("/getMenuList")
    public SzResult getMenuList(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        return SzResult.success(webMenuBu.getWebMenu());
    }
    //查询登录日志
    @PostMapping("/getLoginLog")
    public SzResult getLoginLog(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String page = jsonObject.getString("page");
        String pageSize = jsonObject.getString("pageSize");
        String startTime = jsonObject.getString("startTime");
        String endTime = jsonObject.getString("endTime");
        String user = jsonObject.getString("user");
        String loginType = jsonObject.getString("loginType");
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        //String userName, String startTime, String endTime, String loginType,String page,String pageSize
        return loginBu.loginLog(user,startTime,endTime,loginType,page,pageSize);
    }
}
